#include "FKAppVersionModel.h"

#include "FKLogger.h"
#include "FKModelRole.h"
#include "FKOSType.h"

FKAppVersionModel::FKAppVersionModel(QObject *parent):FKQuickRowModel(parent),
        _filteringExpr(QString("[")+QString::number(FKOSType::curOS)+QString::number(FKOSType::anyOS)+QString("]")){
    FK_CBEGIN
    connect(this,SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)),SLOT(internalDataChanged(QModelIndex,QModelIndex,QVector<int>)));
    refreshDownloadedWeights();
    FK_CEND
}

FKAppVersionModel::~FKAppVersionModel(){
    FK_DBEGIN
    FK_DEND
}

QHash<int,QByteArray> FKAppVersionModel::roleNames()const{
    QHash<int,QByteArray> h(FKQuickRowModel::roleNames());
    h[FKModelRole::needDL]=FKModelRole::fkqNeedDL;
    h[FKModelRole::fileVName]=FKModelRole::fkqFileVName;
    h[FKModelRole::hostVersion]=FKModelRole::fkqHostVersion;
    h[FKModelRole::hostWeight]=FKModelRole::fkqHostWeight;
    h[FKModelRole::clientVersion]=FKModelRole::fkqClientVersion;
    h[FKModelRole::clientWeight]=FKModelRole::fkqClientWeight;
    h[FKModelRole::downloadedSize]=FKModelRole::fkqDownloadedSize;
    h[FKModelRole::needRE]=FKModelRole::fkqNeedRE;
    h[FKModelRole::hostWeightLbl]=FKModelRole::fkqHostWeightLbl;
    h[FKModelRole::clientWeightLbl]=FKModelRole::fkqClientWeightLbl;
    h[FKModelRole::downloadedWeightLbl]=FKModelRole::fkqDownloadedWeightLbl;
    h[FKModelRole::allowDL]=FKModelRole::fkqAllowDL;
    h[FKModelRole::allowRE]=FKModelRole::fkqAllowRE;
    return h;
}

void FKAppVersionModel::internalDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector<int> &roles){
    Q_UNUSED(topLeft)
    Q_UNUSED(bottomRight)
    if(roles.contains(FKModelRole::needDL)){
        refreshDownloadWeights();
    }
}

void FKAppVersionModel::refreshDownloadedWeights(){
    const QVariant t(true);
    qint64 totalDownloaded=0;
    QList<QVariant> downloadedWeights(filterData(FKModelRole::downloadedSize,FKModelRole::needDL,t));
    foreach(QVariant var, downloadedWeights)totalDownloaded+=var.toInt();
    _totalDownloaded=totalDownloaded/1024;
    _totalDownloadedLbl=bytesToLabelText(totalDownloaded);
    emit totalDownloadedChanged(_totalDownloaded);
    emit totalDownloadedLblChanged(_totalDownloadedLbl);
}

void FKAppVersionModel::refreshDownloadWeights(){
    const QVariant t(true);
    qint64 totalDownload=0;
    QList<QVariant> hostWeights(filterData(FKModelRole::hostWeight,FKModelRole::needDL,t));
    foreach(QVariant var, hostWeights)totalDownload+=var.toInt();
    _totalDownload=totalDownload/1024;
    _totalDownloadLbl=bytesToLabelText(totalDownload);
    emit totalDownloadChanged(_totalDownload);
    emit totalDownloadLblChanged(_totalDownloadLbl);
}

QString FKAppVersionModel::bytesToLabelText(const qint64 &bytes){
    if(bytes==0)return QString(tr("N/A"));
    qreal b(bytes);
    QString swl(" b");
    if(b>999){
        b/=1024;
        swl=QString(" kb");
        if(b>1000000){
            b/=1024*1024;
            swl=QString(" Gb");
        }else if(b>1000){
            b/=1024;
            swl=QString(" Mb");
        }
    }
    return QString::number(b,'f',1)+swl;
}

void FKAppVersionModel::setDownloaded(const QString &file, const quint8 &osType, const qint64 &size){
    QModelIndexList lst=match(index(0),FKModelRole::fileVName,file,-1,Qt::MatchExactly);
    foreach (QModelIndex i, lst) {
        if(i.data(FKModelRole::osTypeRole).toUInt()==osType){
            setData(i,size,FKModelRole::downloadedSize);
            setData(i,bytesToLabelText(size),FKModelRole::downloadedWeightLbl);
            break;
        }
    }
    refreshDownloadedWeights();
}

void FKAppVersionModel::setDownloadComplete(const QString &file, const quint8 &osType, const bool &success){
    QModelIndexList lst=match(index(0),FKModelRole::fileVName,file,-1,Qt::MatchExactly);
    foreach (QModelIndex i, lst) {
        if(i.data(FKModelRole::osTypeRole).toUInt()==osType){
            setData(i,success ? QString(tr("Complete")) : QString(tr("Failed")),FKModelRole::downloadedWeightLbl);
            setData(i,!success,FKModelRole::allowDL);
            setData(i,success,FKModelRole::allowRE);
            setData(i,success,FKModelRole::needRE);
            break;
        }
    }
}
